#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/30 16:54
@desc
"""


from __future__ import unicode_literals

import json

import psycopg2
import time
from flask import render_template, request

from setting import hive_db

def getresultexample():
    # job_id = request.args.get('job_id')
    # db_conn = cx_Oracle.connect(hive_db)
    db_infos = gethiveconninfo(hive_db)
    db_conn = psycopg2.connect(database=db_infos.get('database'), user=db_infos.get('username'),
                               password=db_infos.get('password'), host=db_infos.get('host'), port=db_infos.get('port'))
    cursor = db_conn.cursor()
    # sql="SELECT RECORD_EXAMPLE FROM HIVE_JOB WHERE JOB_ID='"+job_id+"'"
    sql = "select job_id,job_type,job_detail,user_name,table_name from hive_job where status='waiting'"
    cursor.execute(sql)
    # record = cursor.fetchone()
    record = cursor.fetchall()
    # try:
    #     result = str(record[0].read()).decode('gbk')
    # except:
    #     result = ''
    cursor.close()
    db_conn.commit()
    db_conn.close()
    # return result
    return record

def gethiveconninfo(login_str):
    db_infos = {}
    dbtype, dbinfo = login_str.split("://")
    loginInfo, hostInfo = dbinfo.split('@')
    userName, passWord = loginInfo.split(':')
    hostInfo, dataBase = hostInfo.split('/')
    host, port = hostInfo.split(':')
    db_infos.setdefault('dbtype', dbtype)
    db_infos.setdefault('username', userName)
    db_infos.setdefault('password', passWord)
    db_infos.setdefault('host', host)
    db_infos.setdefault('port', port)
    db_infos.setdefault('database', dataBase)
    return db_infos

ret = getresultexample()

g = (i for i in ret if (i[2] is not None and i[2] != ""))
#    print(i[2])
for d in g:
    print(d)

